import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseArrayPipe,
  ParseIntPipe,
  Post,
  Put,
  Query,
  Req,
  Res,
} from '@nestjs/common';
import { ParseIntArrayPipe } from '@/common/pipe/parse-int-array.pipe';
import Result from '@/common/result/Result';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { RequirePermission } from '@/common/decorator/require-premission.decorator';
import { nowDateTime } from '@/common/utils';
import { RentalHouseService } from './service/rental-house.service';
import {
  QueryRentalHouseDto,
  CreateRentalHouseDto,
  UpdateRentalHouseDto, createOrderDto,
} from './dto/index';
import { Response } from 'express';
import { RentalHouse } from '@prismaClient';
import { TableDataInfo } from '@/common/domain/TableDataInfo';
@ApiTags('房源表')
@ApiBearerAuth()
@Controller('system/house')
export class RentalHouseController {
  constructor(private houseService: RentalHouseService) {}

  @ApiOperation({ summary: '房源订购' })
  @RequirePermission('system:house:purchase')
  @Post('/purchase')
  @ApiResponse({ type: Result<RentalHouse> })
  @ApiBody({ type: createOrderDto })
  async HousePurchase(
      @Req() req, @Body() rentalHouseOrder: createOrderDto
  ): Promise<Result<any>> {
    const userId = req.userId;
    const { message, code } = await this.houseService.HousePurchase(rentalHouseOrder, userId)
    if(code<=200) return Result.ok(null, message);
    else return Result.Error(message, code);
  }

  @ApiOperation({ summary: '查询房源表列表' })
  @ApiResponse({ type: TableDataInfo<RentalHouse> })
  @RequirePermission('system:house:list')
  @Get('/list')
  async listHouse(
    @Query() q: QueryRentalHouseDto,
  ): Promise<TableDataInfo<RentalHouse>> {
    return Result.TableData(await this.houseService.selectHouseList(q));
  }
  @ApiOperation({ summary: '查询房源表所有' })
  @ApiResponse({ type: Result<RentalHouse[]> })
  @RequirePermission('system:house:list')
  @Get('/data')
  async listHouseData(): Promise<Result<RentalHouse[]>> {
    return Result.ok(await this.houseService.selectHouseAll());
  }
  @ApiOperation({ summary: '导出房源表xlsx文件' })
  @RequirePermission('system:house:export')
  @Get('/export')
  async export(@Res() res: Response): Promise<void> {
    return this.houseService.exportHouse(res);
  }
  @ApiOperation({ summary: '查询房源表详细' })
  @ApiResponse({ type: Result<RentalHouse> })
  @RequirePermission('system:house:list')
  @Get('/:id')
  async getHouse(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<Result<RentalHouse>> {
    return Result.ok(await this.houseService.selectHouseById(id));
  }
  @ApiOperation({ summary: '新增房源表' })
  @ApiResponse({ type: Result<RentalHouse> })
  @ApiBody({ type: CreateRentalHouseDto })
  @RequirePermission('system:house:add')
  @Post('/')
  async addHouse(
    @Body() rentalHouse: CreateRentalHouseDto,
    @Req() req,
  ): Promise<Result<RentalHouse>> {
    rentalHouse['createTime'] = nowDateTime();
    rentalHouse['updateTime'] = nowDateTime();
    return Result.ok(await this.houseService.addHouse(rentalHouse, req.userId));
  }
  @ApiOperation({ summary: '修改房源表' })
  @ApiResponse({ type: Result<any> })
  @ApiBody({ type: UpdateRentalHouseDto })
  @RequirePermission('system:house:edit')
  @Put('/')
  async updateHouse(
    @Body() rentalHouse: UpdateRentalHouseDto,
    @Req() req,
  ): Promise<Result<any>> {
    rentalHouse['updateTime'] = nowDateTime();
    await this.houseService.updateHouse(rentalHouse);
    return Result.ok('修改成功！');
  }
  @ApiOperation({ summary: '删除房源表' })
  @ApiResponse({ type: Result<any> })
  @RequirePermission('system:house:remove')
  @Delete('/:ids')
  async delHouse(
    @Param('ids', ParseIntArrayPipe) ids: number[],
  ): Promise<Result<any>> {
    let { count } = await this.houseService.deleteHouseByIds(ids);
    return Result.toAjax(count);
  }
}
